/**
* Copyright 2013 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.server.database;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nabla.wapp.server.json.SqlToJson;

public class SqlProperties {

	private static final Log	log = LogFactory.getLog(SqlProperties.class);
	private final Properties	resource = new Properties();

	public SqlProperties(final InputStream is) {
		try {
			resource.loadFromXML(is);
		} catch (InvalidPropertiesFormatException e) {
			log.error("failed to load sql resource file", e);
		} catch (IOException e) {
			log.error("failed to load sql resource file", e);
		}
	}

	public String load(final String name) {
		return resource.getProperty(name).trim();
	}

	public SqlToJson sqlToJson(final String name) {
		return new SqlToJson(load(name));
	}

	public SqlToJson sqlToJson(final String name, final String[] parameterNames) {
		return new SqlToJson(load(name), parameterNames);
	}

	public ResultSet execute(final Statement stmt, final String sql) throws SQLException {
		return stmt.executeQuery(load(sql));
	}

	public boolean execute(final Connection conn, final String sql, final Object... parameters) throws SQLException {
		return Database.executeUpdate(conn, load(sql), parameters);
	}

	public Integer addRecord(final Connection conn, final String sql, final Object... parameters) throws SQLException {
		return Database.addRecord(conn, load(sql), parameters);
	}

	public Integer addUniqueRecord(final Connection conn, final String sql, final Object... parameters) throws SQLException {
		return Database.addUniqueRecord(conn, load(sql), parameters);
	}

	public PreparedStatement prepare(final Connection conn, final String sql) throws SQLException {
		return StatementFormat.prepare(conn, load(sql));
	}

	public PreparedStatement prepare(final Connection conn, final String sql, Object... parameters) throws SQLException {
		return StatementFormat.prepare(conn, load(sql), parameters);
	}

	public  PreparedStatement prepare(final Connection conn, int autoGeneratedKeys, final String sql) throws SQLException {
		return StatementFormat.prepare(conn, autoGeneratedKeys, load(sql));
	}

	public  PreparedStatement prepare(final Connection conn, int autoGeneratedKeys, final String sql, Object... parameters) throws SQLException {
		return StatementFormat.prepare(conn, autoGeneratedKeys, load(sql), parameters);
	}

	public static PreparedStatement prepare(final Connection conn, int autoGeneratedKeys, final String sql, final String[] parameterNames, Object... parameterValues) throws SQLException {
		return StatementFormat.prepare(conn, autoGeneratedKeys, sql, parameterNames, parameterValues);
	}

	public static PreparedStatement prepare(final Connection conn, final String sql, final String[] parameterNames, Object... parameterValues) throws SQLException {
		return StatementFormat.prepare(conn, sql, parameterNames, parameterValues);
	}

	public  LockTableGuard lock(final Connection conn, final String sql) throws SQLException {
		return new LockTableGuard(conn, load(sql));
	}

}
